package com.risen.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.risen.bean.Book;
import com.risen.dao.BookMapper;
import com.risen.util.Paging;

/**
 * @author sen
 *	图书模块service层
 */

@Service
public class BookService {
	
	private static final Integer pageStart = 1;
	
	private static final Integer pageSize = 10;
	
	@Resource
	private BookMapper bookMapper;
	
	
	/**
	 * @param cid
	 * @param paging
	 * @return
	 *  
	 */
	public List<Book> queryBookList(Book book,Paging paging){
		//sql语句需要的参数map，包括查询条件(封装到book对象中)和分页的limit参数(封装到paging对象中)
		Map<String,Object> params=new HashMap<String, Object>();
		params.put("book", book);
		params.put("paging",paging);//setCurrentPage方法自动计算了limit参数（controller已调用）
		List<Book> bookList=bookMapper.queryBookList(params);//得到查询结果集
		
		//设置paging的totalItems值
		paging.setTotalItems(bookMapper.getTotalItems(book));
		
		return bookList;
	}
	
	
	/**
	 * @param bid
	 * @return
	 */
	public Book queryByBid(int bid){
		return bookMapper.queryByBid(bid);
		
	}

	
	/**
	 * @param cid
	 * @return
	 *  查询主页默认的图书列表
	 */
	public List<Book> queryHomePageList(int cid) {
		return bookMapper.queryHomePageList(cid);
	}
	
	public Map<String, Object> findAll(String cid, Integer cur, Integer size) {
		Map<String, Object> result = new HashMap<String, Object>();
		int realPage = cur;  //当前页
		int realSize = size; //每页大小
		if (size == null) {
			realSize = pageSize;
		}
		if (cur == null) {
			realPage = pageStart;
		}
		int start = (realPage - 1) * realSize;
		List<String> idList = new ArrayList<String>();
		//查询孩子id的集合
		if (cid !=null && !"".equals(cid)) {
			//idList = findAllChild(cid);          //使用循环遍历实现
			idList = bookMapper.findChildByProcedure(cid); //使用存储过程实现
		}
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("idList", idList);
		param.put("start", start);
		param.put("realSize", realSize);
		result.put("data", bookMapper.findAll(param));
		result.put("totalCount", bookMapper.count(param));
		return result;
	}
	
	/**
	 * 找到所有的孩子
	 * @param cid
	 * @return
	 */
	public List<String> findAllChild(String cid) {
		List<String> allId = new ArrayList<String>(); //存放全部孩子id
		allId.add(cid);
		List<String> childIdList = bookMapper.findChild(cid); 
		allId.addAll(childIdList); //添加第一层查询到的孩子id
		List<String> storage = new ArrayList<String>(); //存放一轮遍历中找到的所有孩子id,用于第二轮循环
		while (childIdList != null && childIdList.size() > 0) {
			for (String ci : childIdList) {
				List<String> childInner = bookMapper.findChild(ci);
				if (childInner !=null && childInner.size() > 0) {
					storage.addAll(childInner); 
					allId.addAll(childInner);
				}
			}
			childIdList.clear(); 
			childIdList.addAll(storage); //将找到的孩子id赋给childIdList，作为新的while循环条件
			storage.clear();
		}
		return allId;
	}
	
	/**
	 * 新增书籍
	 * @param book
	 * @return
	 */
	public boolean save(Book book) {
		try {
			bookMapper.save(book);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	
	public boolean edit(Book book) {
		try {
			bookMapper.edit(book);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	
	/**
	 * 删除书籍
	 * @param bid
	 * @return
	 */
	public boolean deleteBook(int bid) {
		try {
			bookMapper.deleteBook(bid);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	
}
